<template>
    <div class="app-container">
        <div class="search-container">
            <el-form ref="queryFormRef" :model="queryParams" :inline="true">

                <el-form-item style="margin-right: 15px;">
                    <el-input v-model="queryParams.name" placeholder="艺术品标题名字"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
                    <el-button @click="add()">新增</el-button>
                    <el-button @click="saveSort()">保存排序</el-button>
                </el-form-item>





            </el-form>
        </div>

        <el-card shadow="never" class="table-container">
            <el-table :data="artworkPageData" style="width: 100%" border>
                <el-table-column prop="name" label="艺术品名称" align="center" />
                <el-table-column prop="artistName" label="艺术家" align="center" />
                <el-table-column label="艺术品" align="center">
                    <template #default="scope">
                        <img v-if="scope.row.url !== null && scope.row.url !== ''" :src="scope.row.url"
                            style="width: 100px;height: 100px;">
                    </template>
                </el-table-column>

                <el-table-column prop="sort" label="排序" align="center">
                    <template #default="scope">
                        <el-input-number v-model="scope.row.sort" />
                    </template>
                </el-table-column>

                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button plain type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
        </el-card>

        <!-- 修改弹框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="90%">
            <div style="display: flex;">
                <div style="flex: 1;padding-left: 10px">
                    <div style="margin-bottom: 10px;">
                        <el-input v-model="queryArtworkParams.name" placeholder="搜索艺术品名字"
                            style="width: 200px;margin-right: 10px"></el-input>
                        <el-input v-model="queryArtworkParams.artistName" placeholder="搜索艺术家名字"
                            style="width: 200px;margin-right: 10px"></el-input>
                        <el-input v-model="queryArtworkParams.artworkNum" placeholder="搜索艺术品编号"
                            style="width: 200px;margin-right: 10px"></el-input>
                        <el-button @click="initNotInArtworkPageData">搜索</el-button>
                    </div>
                    <div style="width: 100%;">
                        <el-table width="100%" style="height: 400px;" :data="notInArtworkPageData" highlight-current-row
                            border>
                            <el-table-column label="艺术品名称" prop="name" />
                            <el-table-column label="编号" prop="artworkNum" />
                            <el-table-column label="标签" prop="name" />
                            <el-table-column label="图片">
                                <template #default="scope">
                                    <img v-if="scope.row.url !== null && scope.row.url !== ''" :src="scope.row.url"
                                        alt="" style="width: 100px;height: 100px;">
                                </template>
                            </el-table-column>
                            <el-table-column label="操作">
                                <template #default="scope">
                                    <el-button type="primary"
                                        @click="addArtworkToContent(scope.row)">艺术品加入首页模块</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <pagination v-if="artworkTotal > 0" v-model:total="artworkTotal"
                            v-model:page="queryArtworkParams.pageNum" v-model:limit="queryArtworkParams.pageSize"
                            @pagination="initNotInArtworkPageData" />
                    </div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">


// 艺术品接口
import ArtworkApi from "@/api/artwork";
import { ArtworkQuery, ArtworkPageResult, ArtworkForm, ArtworkPageVO, IndexItemRelativeQuery, IndexItemRelativeForm } from "@/api/artwork/model"

defineOptions({
    name: "Banner",
    inheritAttrs: false,
});





const queryFormRef = ref(ElForm);
// 首页模块id
const indexTypeId = ref("830388e7-538a-45f5-9201-cf2c67117674")
// 查询参数
const type = ref(3)

// 艺术品分页查询参数
const queryArtworkParams = reactive<ArtworkQuery>({
    pageNum: 1,
    pageSize: 10,
    departId: "",
});
// 艺术品分页数据
const inArtworkPageData = ref<ArtworkPageVO[]>();
const notInArtworkPageData = ref<ArtworkPageVO[]>();


const editForm = reactive<ArtworkForm>({

});
const artworkTotal = ref(0)

const loading = ref(false);
const editDialogVisible = ref(false);


const total = ref(0);
// 搜索参数
const queryParams = reactive<IndexItemRelativeQuery>({
    pageNum: 1,
    pageSize: 10,
    name: '',
    indexItemId: '',
});

const indexItemRelativeQueryParams = reactive<IndexItemRelativeQuery>({
    pageNum: 1,
    pageSize: 10,
    name: '',
})

const artworkPageData = ref<ArtworkPageVO[]>();

const dialog = reactive({
    title: "",
    visible: false,
});

/**
 * 打开新增弹框
 */
function add() {
    editForm.id = null
    editForm.createUser = null;
    editForm.createTime = null;
    editForm.updateTime = null;
    editForm.sort = null;
    dialog.visible = true;
    // 获取未关联数据
    initNotInArtworkPageData();
}
/**
 * 保存排序
 */
function saveSort() {
    let arr: any[] = []
    artworkPageData.value!.forEach((item: any) => {
        arr.push({
            indexItemId: indexItemRelativeQueryParams.indexItemId,
            contentId: item.artworkId,
            sort: item.sort
        })
    })
    ArtworkApi.saveSort(arr).then(res => {
        ElMessage.success("保存排序成功")
        handleQuery()
    })
}

/**
 * 获取未关联艺术品
 */
function initNotInArtworkPageData() {
    ArtworkApi.getIndexNoRelativeArtworkPage(indexItemRelativeQueryParams).then(res => {
        console.log(res, "获取艺术品列表")
        notInArtworkPageData.value = res.records;
        res.records.forEach((item: any) => {
        })
        artworkTotal.value = res.total;
    })
}

/** 查询 */
function handleQuery() {
    loading.value = true;
    queryParams.indexItemId = indexTypeId.value
    ArtworkApi.getIndexRelativeArtworkPage(queryParams)
        .then((data) => {
            artworkPageData.value = data.records;
            total.value = data.total;
        })
        .finally(() => {
            loading.value = false;
        });
}


/**
 * 艺术品
 * @param row 
 */
function addArtworkToContent(row: any) {
    let params = {
        sort: 0,
        indexItemId: indexItemRelativeQueryParams.indexItemId,
        type: indexItemRelativeQueryParams.type,
        contentId: row.artworkId,
    }
    ArtworkApi.addToIndexRelative(params).then(res => {
        initNotInArtworkPageData()
    })


}

function handleDelete(row: any) {
    let obj = {
        indexItemId: indexItemRelativeQueryParams.indexItemId,
        contentId: row.artworkId,
    }
    ArtworkApi.deleteIndexItemRelative(obj).then(res => {
        ElMessage.success("删除成功")
        handleQuery()
    })

}









onMounted(() => {
    indexItemRelativeQueryParams.indexItemId = indexTypeId.value
    indexItemRelativeQueryParams.type = type.value
    handleQuery();
});
</script>